<script setup>
import { ElMessage, ElMessageBox } from 'element-plus'


import { Status } from '@/utils/enum'

import { pageApi, logoutApi } from '@/api/online'

const refContainerSidebar = ref()
const refTable = ref()

const loading = ref(false)
const visible = ref(false)
const form = reactive({

})
const page = reactive({
  current: 1,
  size: 10,
  total: 0
})
const list = ref([])

/**
 * @description: 获取分页列表
 * @param {*}
 * @return {*}
 */
const getList = () => {
  const { current, size } = page
  const params = {
    ...form,
    current,
    size
  }
  loading.value = true
  pageApi(params).then(r => {
    if (r) {
      list.value = r.data.list
      page.total = r.data.total
    }
    nextTick(() => {
      loading.value = false
    })
  })
}

/**
 * @description: 强制退出
 * @param {row} row
 * @return {*}
 */
const logoutHandle = row => {
  ElMessageBox.confirm(`确定对[${row.userName}]进行[强制退出]操作?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    logoutApi(row.token).then(r => {
      if (r) {
        ElMessage({
          message: '操作成功!',
          type: 'success'
        })
        getList()
      }
    })
  }).catch(() => {
    // to do something on canceled
  })
}

const changeHandle = _val => {
  refContainerSidebar.value.setScrollTop()
  getList()
}

onBeforeMount(() => {
  getList()
})
</script>

<template>
  <ContainerSidebar ref="refContainerSidebar" :scroll="false">

    <template #default>
      <el-table ref="refTable" v-loading="loading" :data="list" border>
        <el-table-column align="center" label="ID" prop="id" width="80" show-overflow-tooltip />
        <el-table-column align="center" label="头像" prop="avatar" width="80">
          <template v-slot="{ row }">
            <el-image v-if="row.avatar" class="height-50 width-50" fit="cover" :src="row.avatar" preview-teleported
              :preview-src-list="[row.avatar]" />
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column label="会话编号" align="center" prop="tokenId" show-overflow-tooltip />
        <el-table-column label="登录名称" align="center" prop="userName" show-overflow-tooltip />
        <el-table-column label="部门名称" align="center" prop="deptName" />
        <el-table-column label="主机" align="center" prop="ipaddr" show-overflow-tooltip />
        <el-table-column label="登录地点" align="center" prop="loginLocation" show-overflow-tooltip />
        <el-table-column label="浏览器" align="center" prop="browser" />
        <el-table-column label="操作系统" align="center" prop="os" />
        <el-table-column label="登录时间" align="center" prop="loginTime" show-overflow-tooltip>
          <template v-slot="{ row }"><span>{{ parseTime(row.loginTime) }}</span></template>
        </el-table-column>

        <el-table-column align="center" label="操作" width="80" fixed="right" v-permission="'online:logout'">
          <template v-slot="{ row }">
            <el-button v-permission="'online:logout'" type="danger" link @click="logoutHandle(row)">强制退出</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <template #footer>
      <Page v-model:current="page.current" v-model:size="page.size" :total="page.total" @change="getList" />
    </template>
  </ContainerSidebar>
</template>
